﻿using Google.Protobuf.Collections;
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Qdrant.Client.Grpc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace QdrantConnector
{
    [Icon("pack://application:,,,/QdrantConnector;component/Resources/Icon.png")]
    [Designer("QdrantConnector.Designer.QdrantConnectorServerCommandDesigner, QdrantConnector")]
    [Category("Qdrant向量数据库")]
    [OrderWeight(300)]
    internal class RemoveFromCollection : BaseQdrantOp
    {
        [FormulaProperty]
        [OrderWeight(100)]
        [DisplayName("需删除的ID")]
        public object IdFormula { get; set; }

        public override async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            var id = int.Parse((await dataContext.EvaluateFormulaAsync(this.IdFormula))?.ToString() ?? "-1");


            if (id >= 0)
            {
                var client = await getQrantClient(dataContext);
                var collectionName = await getCollectionName(dataContext);

                dataContext.Log.AppendLine($"开始从Qdrant集合【{collectionName}】中删除ID为【{id}】的向量...");
                var tick = DateTime.Now;

                await client.DeleteAsync(collectionName, (ulong)id);

                dataContext.Log.AppendLine($"Qdrant删除集合【{collectionName}】中ID为【{id}】的向量成功，耗时：{(DateTime.Now - tick).TotalMilliseconds}ms");
            }

            return new ExecuteResult();
        }

        public override string ToString()
        {
            return "Qdrant删除向量";
        }

    }
}
